/**
 * Created by LiuJie on 2017/10/16.
 */
var express = require("express");
var request = require('request');

var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var util = require('../../util/commonutil');
var router = express.Router();
var resultJSON = require('../common/resultJSON');
router.get("/",function (req, res, next) {
    res.render("dataManage/addUserData",{id:req.param("id"),titleTxt:req.param("id") ? "修改" : "新增"});
});

router.post("/saveData",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/sysDept/save",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("新增用户数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == true){
                resultJSON.isSuccess = true;
                resultJSON.data = result.data;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="保存数据异常，请稍后重试！"
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="保存数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});
router.post("/queryUserInfo",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/sysDept/edit",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("新增用户数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == true){
                resultJSON.isSuccess = true;
                resultJSON.data = result.obj;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="查询数据异常，请稍后重试！"
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="查询数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});
router.post("/updateData",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/sysDept/save",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("新增用户数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == true){
                resultJSON.isSuccess = true;
                resultJSON.data = result.data;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="保存数据异常，请稍后重试！"
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="保存数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});
module.exports = router;